package com.newtouch.bxzs.business.statistics.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newtouch.bxzs.business.personal.mapper.PersonalMapper;
import com.newtouch.bxzs.business.statistics.mapper.MemberPushSumMapper;
import com.newtouch.bxzs.business.statistics.mapper.OrganizationPushSumMapper;
import com.newtouch.bxzs.business.statistics.model.bo.MemberPushSumBO;
import com.newtouch.bxzs.business.statistics.model.bo.OrganizationPushSumBO;
import com.newtouch.bxzs.business.statistics.model.vo.MemberPushSumVO;
import com.newtouch.bxzs.business.statistics.model.vo.OrganizationPushSumVO;
import com.newtouch.bxzs.business.statistics.service.MemberPushSumService;
import com.newtouch.bxzs.common.base.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 机构意向推送汇总查询
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/27 17:29</td>
 * <td>lingshouyi</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author lingshouyi
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class MemberPushSumServiceImpl implements MemberPushSumService {

    @Autowired
    private MemberPushSumMapper memberPushSumMapper;

    @Autowired
    private PersonalMapper personalMapper;

    @Autowired
    private OrganizationPushSumMapper organizationPushSumMapper;

    @Override
    public IPage<MemberPushSumBO> getMemberPushSumList(MemberPushSumVO memberPushSumVO) {
        if (BeanUtil.isEmpty(memberPushSumVO)) {
            return null;
        }
        List<String> userIdList = new ArrayList<>();
        if (StringUtils.isEmpty(memberPushSumVO.getPushUserId())){
            /*根据机构编码获取该机构业务员*/
            QueryWrapper<User> qw = new QueryWrapper<>();
            qw.eq("USER_ORGANIZATION_CODE", memberPushSumVO.getOrganizationCode());
            List<User> userList = personalMapper.selectList(qw);
            if (CollectionUtils.isEmpty(userList)) {
                return null;
            }
            if(!StringUtils.isEmpty(memberPushSumVO.getPushUserId())){
                userIdList.add(memberPushSumVO.getPushUserId());
            }else {
                /*获取每个员工id*/
                for (User user : userList) {
                    userIdList.add(user.getUserId());
                }
            }
        }else {
            userIdList.add(memberPushSumVO.getPushUserId());
        }
        /*分页*/
        Page<MemberPushSumBO> page = new Page<>(memberPushSumVO.getCurrentPage(), memberPushSumVO.getPageSize());

        IPage<MemberPushSumBO> memberPushSumList = memberPushSumMapper.getMemberPushSum(page, userIdList, DateUtil.beginOfDay(memberPushSumVO.getStartTime()), DateUtil.endOfDay(memberPushSumVO.getEndTime()));
        Integer n = 1;
        for (MemberPushSumBO memberPushSumBO : memberPushSumList.getRecords()) {
            memberPushSumBO.setRank(n);
            n++;
        }
        return memberPushSumList;

    }

    @Override
    public IPage<OrganizationPushSumBO> getOrganizationPushSumList(OrganizationPushSumVO organizationPushSumVO) {
        if (BeanUtil.isEmpty(organizationPushSumVO)) {
            return null;
        }
        /*分页*/
        Page<OrganizationPushSumBO> page = new Page<>(organizationPushSumVO.getCurrentPage(), organizationPushSumVO.getPageSize());
        /*判断是否传入推送人*/
        if (!StringUtils.isEmpty(organizationPushSumVO.getPushUserId())) {
            /*获取当前推送人的机构编码,将该代码设置到OrganizationCodeList中*/
            QueryWrapper<User> qw = new QueryWrapper<>();
            qw.eq("USER_ID", organizationPushSumVO.getPushUserId());
            /*将推送人的机构编码设置为当前参数*/
            List<String> organizationCodeList = new ArrayList<>();
            organizationCodeList.add(personalMapper.selectList(qw).get(0).getUserOrganizationCode());
            organizationPushSumVO.setOrganizationCodeList(organizationCodeList);
        }
        return organizationPushSumMapper.getOrganizationPushSumList(page, organizationPushSumVO.getOrganizationCodeList(), DateUtil.beginOfDay(organizationPushSumVO.getStartTime()),DateUtil.endOfDay(organizationPushSumVO.getEndTime()));
    }
}
